<template>
    <el-container>
        <el-aside class="leftBox">
            <!-- 搜索 -->
            <div class="classification" v-if="articleConfig.homeissearch">
                <div class="search">
                    <el-input placeholder="请输入文章名称" v-model="resarchData" class="input-with-select">
                        <el-button slot="append" icon="el-icon-search" @click="resarch()">搜索</el-button>
                    </el-input>
                </div>
            </div>
            <!-- 文章分类 -->
            <!-- <div class="classification">
                <div class="classification_inner clearFix">
                    <span class="supervise">文章分类</span>
                    <el-button type="text" class="superviseBtn" @click="dialogVisible = true">管理分类</el-button>
                </div>
                <ul class="class_list">
                    <li class="list" v-for="(item, index) in articleTypeList" :key="index">
                        <p class="clearFix">
                            <span>{{item.name}}</span>
                            <span v-if="item.count">({{item.count}})</span>
                            <span v-else>(0)</span>
                        </p>
                    </li>
                </ul>
            </div> -->
            <!-- 文章日历 -->
            <div class="classification">
                <div class="classification_inner">
                    <span>文章日历</span>
                </div>
                <el-calendar v-if="false" v-model="calendar"></el-calendar>
                <div class="calendar">
                    <div class="year">
                        <i class="el-icon-arrow-left" @click="year -= 1"></i>
                        <span>{{ year }}</span>
                        <i class="el-icon-arrow-right" @click="year += 1"></i>
                    </div>
                    <div class="month clearFix">
                        <div :class="isMonth == item ? 'month_list isMonth' : 'month_list'" v-for="(item, index) in 12"
                            :key="index" @click="goMonth(item)">{{ item }}月</div>
                    </div>
                </div>
            </div>
        </el-aside>
        <!-- tab -->
        <el-container>
            <div class="contentBox">
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane label="他的文章" name="meArticle">
                        <ArticleList ref="meArticle" v-if="articleTab=='meArticle'" :articleTab="articleTab"
                            @cShowPagin="cShowPagin" @articleList="articleList" :articleConfig='articleConfig'></ArticleList>
                    </el-tab-pane>
                </el-tabs>
                <div style="height:52px;"></div>
                <!--分页-->
                <div class="block" v-if="isShowPagin">
                    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="paginationParams.currentPage" :page-sizes="paginationParams.pageSizes"
                        :page-size="paginationParams.pageSize" :layout="paginationParams.layout"
                        :total="paginationParams.total"></el-pagination>
                </div>
            </div>
        </el-container>
    </el-container>
</template>

<script>
    import {
        mapState
    } from "vuex";
    import ArticleList from '@/views/pages/otherZone/articleList'
    export default {
        components: {
            ArticleList
        },
        computed: {
            ...mapState({
                // 我的文章/草稿列表
                articleOrDraftList: state => state.articleModule.articleOrDraftList,
                // 分页插件参数统一配置
                paginationParams: state => state.commonModule.paginationParams,
                // 权限列表
                permissionArr: state => state.commonModule.permissionArr,
                // 分类列表
                articleClassificationList: state =>
                    state.articleModule.articleClassificationList,
                otherUserInfo: state => state.userModule.otherUserInfo
            })
        },
        created(){
            this.articleGetSetting()
        },
        beforeMount() {
            // this.getArticleClassificationList();
        },
        data() {
            return {
                visitjuris: 5, // 所有人
                homeviewcount: 3,
                // 用户的文章配置信息
                articleConfig: {
                    visitjuris: 5, // 所有人
                    homeviewcount: 3,
                    homeissearch: true,
                    homearticlemode: 1,
                    articlejuris: 5,
                    articlereprinttype: "全部分类"
                },
                articleType: {}, // 暂存文章类型
                dialogManageTypeV: false,
                articleTypeList: [], // 文章分类列表
                articleTab: "meArticle",
                isShowPagin: true,
                resarchData: "",
                calendar: new Date(),
                yearandmonth: null,
                year: "",
                isMonth: 0,
                dialogVisible: false,
                // 文章管理输入框内容
                manageTypeI: "",
                modifyType: false,
                addArticelType: false,
                activeName: "meArticle"
            };
        },
        mounted() {
            if(this.$route.query.others){
                this.$refs.meArticle.goReading(this.$route.query.others)
            }
            let date = new Date();
            this.year = date.getFullYear();
            this.articleList();
        },
        methods: {
            // 编辑文章或者草稿
            articleEdit(params) {
                this.activeName = "meArticle";
                this.articleTab = "meArticle";
                this.$nextTick(() => {
                    this.$refs.meArticle.articleEdit(params, 'drafts');
                })
            },
            // 删除文章或者草稿
            articleDelete(params) {
                this.articleTab = "meArticle";
                this.$nextTick(() => {
                    this.$refs.meArticle.articleDelete(params, true);
                })
            },
            // 获取文章显示配置文件
            articleGetSetting(){
                this.$store
                    .dispatch("articleModule/articleGetSetting", {
                        userid: this.otherUserInfo.userid,
                    })
                    .then(res => {
                        if(res.data.code == 200){
                            this.articleConfig = res.data.result
                        }
                    });
            },
            // 保存文章配置文件
            addArticleConfig() {
                this.articleConfig.id = this.otherUserInfo.userid 
                this.$store
                    .dispatch("articleModule/articleSetting", this.articleConfig)
                    .then(res => {
                        if(res.data.code == 200){
                            this.articleGetSetting()
                            this.$message.success('更新设置成功')
                            this.articleTab = 'meArticle'
                            this.activeName = 'meArticle'
                        }
                    });
            },
            // 删除文章分类
            deleteArticleType(params) {
                this.$store
                    .dispatch("articleModule/del", {
                        id: params.id,
                        userid: this.otherUserInfo.userid
                    })
                    .then(res => {
                        if (res.data.code == 200) {
                            this.$message.success("删除成功");
                            this.getArticleClassificationList();
                        }
                    });
            },
            //   打开编辑分类弹框
            openInnerDg(params) {
                this.articleType = params;
                this.dialogManageTypeV = true;
            },
            // 获取文章分类列表
            getArticleClassificationList() {
                this.$store
                    .dispatch("articleModule/articleClassificationList", {
                        hisUserId: this.otherUserInfo.userid
                    })
                    .then(res => {
                        this.articleTypeList = res.data.result;
                    });
            },
            // 修改文章类型
            modifyTypeM(params) {
                if (this.manageTypeI && this.manageTypeI != "") {
                    this.$store
                        .dispatch("articleModule/articleClassificationSave", {
                            userid: this.otherUserInfo.userid,
                            id: this.articleType.id,
                            name: this.manageTypeI
                        })
                        .then(res => {
                            if (res.data.code == 200) {
                                this.$message.success("修改成功");
                                this.getArticleClassificationList();
                                this.manageTypeI = "";
                                this.dialogManageTypeV = false;
                            }
                        });
                } else {
                    this.$message.warning("输入不能为空");
                }
            },
            // 增加文章类型
            addArticelTypeM() {
                this.$store
                    .dispatch("articleModule/articleClassificationSave", {
                        userid: this.otherUserInfo.userid,
                        id: null,
                        name: this.manageTypeI
                    })
                    .then(res => {
                        if (res.data.code == 200) {
                            this.manageTypeI = "";
                            this.addArticelType = false;
                            this.getArticleClassificationList();
                        }
                    });
            },
            // 是否显示分页插件
            cShowPagin(param) {
                this.isShowPagin = param;
            },
            // 搜索时触发
            resarch() {
                this.articleTab = "meArticle";
                this.activeName = "meArticle";
                this.articleList()
            },
            // 日历文章查询
            goMonth(index) {
                if(this.isMonth == index){
                    this.isMonth = 0
                    this.yearandmonth = null
                }else {
                    this.isMonth = index;
                    this.yearandmonth = this.year + "-" + (index >= 10 ? index : "0" + index);
                }
                this.articleList();
                this.articleTab = "meArticle";
                this.activeName = "meArticle";
            },
            // 获取好友文章
            articleFriendList() {
                this.$store
                    .dispatch("articleModule/articleFriendList", {
                        userid: this.otherUserInfo.userid
                    })
                    .then(res => {
                        this.$store.commit("commonModule/INIT_PAGINATIONPARAMS", {
                            val: res.data.result.total,
                            type: "total"
                        });
                    });
            },
            // 获取草稿/文章列表
            articleList(param) {
                this.$store
                    .dispatch("articleModule/articleList", {
                        limit: this.paginationParams.pageSize,
                        page: this.paginationParams.currentPage,
                        search: this.resarchData,
                        classifyid: null,
                        yearandmonth: this.yearandmonth,
                        draft: param ? 0 : 1,
                        userid: this.otherUserInfo.userid || this.otherUserInfo.id
                    })
                    .then(res => {
                        if (res.data.code == 200) {
                            this.$store.commit("commonModule/INIT_PAGINATIONPARAMS", {
                                val: res.data.result.total,
                                type: "total"
                            });
                        }
                    });
            },
            // 获取收藏文章
            collectPage() {
                this.$store.dispatch('collectModule/collectPage', {
                    id: this.otherUserInfo.userid, //主体id：主键id，评论回复的时候就是评论id
                    limit: this.paginationParams.pageSize,
                    page: this.paginationParams.currentPage,
                }).then(res =>{
                    if(res.data.code == 200){
                        this.$store.commit("commonModule/INIT_PAGINATIONPARAMS", {
                            val: res.data.result.total,
                            type: "total"
                        });
                        this.$store.commit('articleModule/INIT_ARTICLEORDRAFTLIST', res.data.result.list);
                    }
                })
            },
            // 获取当前用户的文章显示设置
            // tab 切换时显示
            handleClick(tab, event) {
                this.yearandmonth = null;
                this.isMonth = 0;
                this.isShowPagin = true;
                this.articleTab = tab.name;
                if (tab.name == "drafts") {
                    this.articleList(true);
                }
                if (tab.name == "meArticle") {
                    this.articleList();
                }
                // 当权限切换到设置时获取当前用户之前设置过的权限
                if (tab.name == "setUp") {}
                // 获取好友列表
                if (tab.name == "friendArticle") {
                    this.articleFriendList();
                }
                // 获取收藏列表
                if (tab.name == "collection") {
                    this.collectPage();
                }
            },
            handleSizeChange(val) {
                this.$store.commit("commonModule/INIT_PAGINATIONPARAMS", {
                    val: val,
                    type: "pageSize"
                });
                if (this.articleTab == "meArticle") {
                    this.articleList();
                }
                if (this.articleTab == "drafts") {
                    this.articleList(true);
                }
            },
            handleCurrentChange(val) {
                this.$store.commit("commonModule/INIT_PAGINATIONPARAMS", {
                    val: val,
                    type: "currentPage"
                });
                if (this.articleTab == "meArticle") {
                    this.articleList();
                }
                if (this.articleTab == "drafts") {
                    this.articleList(true);
                }
                
            },
            handleSelect(key, keyPath) {
            }
        }
    };
</script>

<style lang="scss" scoped>
    .leftBox {
        background: #fff;
        padding: 5px;
        width: 240px;

        .classification {
            box-sizing: border-box;
            padding: 15px 0;
            border-bottom: 1px solid #909399;

            .classification_inner {
                font-size: 16px;
                color: #303133;
                text-align: left;
                margin-bottom: 10px;

                .supervise {
                    float: left;
                    margin-top: 10px;
                }

                .superviseBtn {
                    float: right;
                    font-size: 16px;
                }
            }

            .search {
                width: 290px;
            }

            .class_list {
                margin-block-start: 0;
                margin-block-end: 0;
                padding-inline-start: 0;

                .list {
                    margin: 5px 0;
                    border-radius: 5px;

                    p {
                        box-sizing: border-box;
                        padding: 5px;
                        margin-block-start: 0;
                        margin-block-end: 0;
                        background-color: #f5f7fa;
                        font-size: 14px;
                        color: #909399;
                        cursor: pointer;
                    }

                    p>span:first-of-type {
                        float: left;
                    }

                    p>span:last-of-type {
                        float: right;
                    }
                }
            }

            .calendar {
                .year {
                    box-sizing: border-box;
                    padding: 12px 0;
                    text-align: center;
                    border: 1px solid #ebeef5;
                    border-bottom: 0;
                    -moz-user-select: none;
                    -webkit-user-select: none;
                    -ms-user-select: none;
                    user-select: none;

                    span {
                        color: #5e6d82;
                        margin: 0 10px;
                    }

                    i {
                        cursor: pointer;
                    }
                }

                .month {
                    box-sizing: border-box;
                    /*padding: 12px 0;*/
                    border-left: 1px solid #ebeef5;
                    border-top: 1px solid #ebeef5;
                    display: flex;
                    flex-wrap: wrap;
                    align-items: center;

                    .month_list {
                        white-space: nowrap;
                        width: 25%;
                        box-sizing: border-box;
                        padding: 14px 12px;
                        font-size: 12px;
                        text-align: center;
                        color: #5e6d82;
                        cursor: pointer;
                        border-right: 1px solid #ebeef5;
                        border-bottom: 1px solid #ebeef5;
                        transition: background-color 0.4s;
                    }

                    .isMonth {
                        background-color: #409eff;
                        color: white;
                    }
                }
            }
        }

        .classification:last-of-type {
            border-bottom: 0;
        }
    }

    .contentBox {
        background: #fff;
        padding: 5px;
        width: 100%;
        min-height: 100vh;
        margin-left: 20px;
        position: relative;

        .content {
            box-sizing: border-box;
            padding: 0 10px;
        }

        .drafts {
            text-align: left;
            font-size: 14px;
            color: #303133;
            box-sizing: border-box;
            padding-left: 10px;

            .abstractBox {
                list-style-type: none;
                margin-block-start: 0;
                margin-block-end: 0;
                padding-inline-start: 0;

                .abstractList {
                    box-sizing: border-box;
                    padding-bottom: 5px;
                    border-bottom: 1px dashed #909399;
                    margin-bottom: 10px;

                    .abstractTitle {
                        font-size: 14px;
                        font-weight: bold;
                    }

                    .abstract {
                        text-indent: 24px;
                        text-align: justify;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        display: -webkit-box;
                        -webkit-line-clamp: 3;
                        -webkit-box-orient: vertical;
                    }
                }
            }
        }

        .setupBox {
            text-align: left;
            font-size: 14px;
            color: #303133;
            box-sizing: border-box;
            padding-left: 10px;

            .setupUl {
                .setTips {
                    font-size: 14px;
                    font-weight: bold;
                }

                list-style-type: none;
                margin-block-start: 0;
                margin-block-end: 0;
                padding-inline-start: 0;

                .setupList {
                    box-sizing: border-box;
                    padding-bottom: 10px;
                    border-bottom: 1px dashed #909399;
                    margin-bottom: 10px;

                    .setBox {
                        padding-left: 10px;
                        margin-top: 10px;
                    }
                }
            }
        }

        .block {
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            box-sizing: border-box;
            padding: 10px 0;
        }
    }

    .clearFix::after {
        content: "";
        display: block;
        font-size: 0;
        width: 100%;
        clear: both;
    }

    .popup {
        width: 100%;
        height: 55vh;
        overflow-y: auto;

        .sortList {
            margin-block-start: 0;
            margin-block-end: 0;
            padding-inline-start: 0;
            list-style-type: none;
            box-sizing: border-box;

            .list {
                padding: 10px 15px;
                text-align: left;
                background-color: #ebeef5;
                color: #303133;
                font-size: 14px;
                margin-bottom: 10px;

                .sortName {
                    float: left;
                    margin-top: 12px;
                }

                .sortBtn {
                    float: right;

                    .delete {
                        margin-left: 10px;
                    }
                }
            }
        }

        .addSort,
        .reminder {
            text-align: left;
        }
    }

    .el-dropdown-link {
        cursor: pointer;
        color: #409eff;
    }

    .el-icon-arrow-down {
        font-size: 12px;
    }

    .demonstration {
        display: block;
        color: #8492a6;
        font-size: 14px;
        margin-bottom: 20px;
    }
</style>